const { User, Survey, Score } = require("../models");
let auth = require("../utils/auth");
let auth1 = require("../utils/auth1");


//访问登录界面get请求
let login_fn = async (ctx, next) => {

   await ctx.render('login');

}

let register_fn = async (ctx, next) => {
   await ctx.render("register.html");
}

//提交登录post请求
let loginDone_fn = async (ctx, next) => {
   let username = ctx.request.body.username;
   let password = ctx.request.body.password;
   console.log(username);
   console.log(password);
   let result2 = '';


   if (username.length > 0 && password.length > 0) {
      let u3 = await User.findOne({
         where: {
            username: username,
            password: password
         }
      }).then((row) => {
         //字符串
         let user = JSON.stringify(row);
         console.log(user);
         //转换为object 
         let userid = JSON.parse(user);
         console.log(userid);
         //数据库中已经注册了该用户
         if (user !== "null") {
            if (username === "milu") {
               ctx.session.username = 'milu';
               ctx.session.userid = row.id;
               result2 = { resultCode: 200, msg: "登录成功" };

            } else {
               ctx.session.username1 = row.username;
               ctx.session.userid1 = row.id;
               result2 = { resultCode: 200, msg: "登录成功" };


            }

         } else {
            result2 = { resultCode: 1000, msg: "登陆失败" }
         }
      })

   }
   ctx.body = JSON.stringify(result2);
}


//提交注册post请求
let registerDone_fn = async (ctx, next) => {
   let username = ctx.request.body.username;
   let password = ctx.request.body.password;
   let againpassword = ctx.request.body.againpassword;
   console.log(username);
   console.log(password);
   console.log(againpassword);

   if (username.length > 0 && password.length > 0 && password === againpassword) {
      let u1 = await User.findOne({
         where: { username: username }

      });
      console.log(u1);
      console.log(JSON.stringify(u1));
      let result = '';
      //当user不为空时表示里面有这条数据
      if (u1) {
         result = { resultCode: 1000, msg: "注册失败" };
      } else {
         let u2 = User.create({
            username: username,
            password: password
         });
         result = { resultCode: 200, msg: "注册成功" };
         //  await ctx.render("login");
         // ctx.redirect("login");
      }
      ctx.body = result;


   }
   else {
      console.log("用户名或者密码不能为空 并且两次密码相同")
   }

}

//注销登录
let logout_fn = async (ctx, next) => {
   ctx.session.username = "";
   ctx.session.username1 = "";
   await ctx.redirect("/login")
}


let homepage_fn = async (ctx, next) => {

   await ctx.render('homepage')

}


let questioncontrol = async (ctx, next) => {

   let control = JSON.parse(JSON.stringify(await Survey.findAll()));

   let obj = { cons: control };

   console.log(control);

   await ctx.render('questioncontrol', obj)

};



let questionserver = async (ctx, next) => {

   await ctx.render('questionserver')

};




let frontdesk_fn = async (ctx, next) => {

   let quest = JSON.parse(JSON.stringify(await Survey.findAll()));


   let personer = ctx.session.username1


   let obj = { questions: quest, personer };

   console.log(quest);

   await ctx.render('frontdesk', obj)


};


let create = async (ctx, next) => {
   await ctx.render('create')

};



let droprow_fn = async (ctx, next) => {

   let id = ctx.request.body.qid;

   console.log(id, '看下id');



   if (id) {
      Survey.destroy({
         where: {
            id: id
         }
      });

      res = { code: 200, msg: '删除成功' };

   } else {
      res = { code: 1000, msg: '删除失败' };


   }

   ctx.body = JSON.stringify(res);


}


//添加问题post请求
let createDone = async (ctx, next) => {

   let title = ctx.request.body.title || '';
   let desc = ctx.request.body.desc || '';
   let que1 = ctx.request.body.que1 || '';
   let que2 = ctx.request.body.que2 || '';
   let que3 = ctx.request.body.que3 || '';
   let que4 = ctx.request.body.que4 || '';
   let que5 = ctx.request.body.que5 || '';
   let userId = ctx.session.userid;
   let time = new Date().toLocaleString('zh-CN');

   console.log(title);
   console.log(desc);
   console.log(que1);
   console.log(que2);
   console.log(que3);
   console.log(que4);
   console.log(que5);
   console.log(userId);
   console.log(time);


   if (que1.length > 0 && que2.length > 0 && que3.length > 0 && que4.length > 0 && que5.length > 0 && title.length > 0 && desc.length > 0) {
      let sur = await Survey.create({
         fromUserId: userId,
         title: title,
         instruction: desc,
         que1: que1,
         que2: que2,
         que3: que3,
         que4: que4,
         que5: que5,
         time: time

      })
      let data = JSON.stringify(sur)
      res = { code: 200, data, msg: '创建成功' };
   } else {
      res = { code: 1000, msg: '创建失败' };
   }

   ctx.body = JSON.stringify(res);
}


//修改密码
let setting_fn = async (ctx, next) => {

   await ctx.render("setup")
}

//修改密码post请求
let settingDone_fn = async (ctx, next) => {
   let password = ctx.request.body.password;
   let againpassword = ctx.request.body.againpassword;
   console.log(password);
   console.log(againpassword);

   if (password.length > 0 && password === againpassword) {
      let username1 = ctx.session.username1;
      let password = ctx.request.body.password;

      let pa1 = username1;
      let password1 = password;


      console.log(password1);
      console.log(pa1)

      await User.update({ password: password1 }, {
         where: {
            username: pa1
         }
      });

      await ctx.redirect("/login");


   }
   else {
      ctx.body = "用户名或者密码不能为空 并且两次密码相同";
      console.log("用户名或者密码不能为空 并且两次密码相同")

   }
}



let questionccid = '';

let writequestion_fn = async (ctx, next) => {

   questionccid = ctx.request.body.sid;

   ctx.body = { code: 200 }


}


let writequestioned_fn = async (ctx, next) => {

   //怎么样去获取到id?  注意是findOne  查询的是单条

   let quest = JSON.parse(JSON.stringify(await Survey.findOne({

      where: { id: questionccid }
   })));

   let obj = { questions: quest };


   console.log(quest);

   await ctx.render('writequestion', obj)

};


let ssid = '';


let questiondetail_fn = async (ctx, next) => {

   ssid = ctx.request.body.ssid;

   ctx.body = { code: 200 }

};


let questiondetailed_fn = async (ctx, next) => {

   let quest = JSON.parse(JSON.stringify(await Survey.findOne({

      where: { id: ssid }
   })));

   let obj = { questions: quest };


   console.log(quest);

   await ctx.render('questiondetail', obj)

}


let ccid = '';

let questioncount_fn = async (ctx, next) => {

   ccid = ctx.request.body.ccid;

   ctx.body = { code: 200 }

};





let questioncounted_fn = async (ctx, next) => {

   let score = JSON.parse(JSON.stringify(await Score.findAll({

      where: { surId: ccid }

   })));




   let obj = { scores: score };

   await ctx.render('questioncount', obj)

};



let addscore_fn = async (ctx, next) => {
   let data = {
      'userId': ctx.session.userid1,
      'surId': ctx.request.body.surId,
      'personer' :ctx.session.username1,
      'sco1': ctx.request.body.sco1,
      'sco2': ctx.request.body.sco2,
      'sco3': ctx.request.body.sco3,
      'sco4': ctx.request.body.sco4,
      'sco5': ctx.request.body.sco5,
      'sumscore': parseInt(ctx.request.body.sco1) + parseInt(ctx.request.body.sco2)+ parseInt(ctx.request.body.sco3)
      + parseInt(ctx.request.body.sco4)+ parseInt(ctx.request.body.sco5)

   }

   console.log(ctx.request.username1);
   console.log(ctx.session.userid1);
   console.log(ctx.request.body.surId)
   console.log(ctx.request.body.sco1);
   console.log(ctx.request.body.sco2);
   console.log(ctx.request.body.sco3);
   console.log(ctx.request.body.sco4);
   console.log(ctx.request.body.sco5);

   let u1 = JSON.parse(JSON.stringify(await Score.create(data)));

   ctx.body = { code: 200, msg: '成功插入' }


}



module.exports = {
   "/login": ["get", auth, login_fn],
   "/register": ["get", register_fn],
   "/loginDone": ["post", loginDone_fn],
   "/registerDone": ["post", registerDone_fn],
   "/": ["get", auth, homepage_fn],
   "/frontdesk": ["get", auth1, frontdesk_fn],
   "/create": ["get", auth, create],
   "/createDone": ["post", auth, createDone],
   "/homepage": ["get", auth, homepage_fn],
   "/questioncontrol": ["get", auth, questioncontrol],
   "/questionserver": ["get", auth, questionserver],
   "/questiondetail": ["post", auth, questiondetail_fn],
   "/questiondetailed": ["get", auth, questiondetailed_fn],
   "/questioncount": ["post", auth, questioncount_fn],
   "/questioncounted": ["get", auth, questioncounted_fn],
   "/logout": ["get", logout_fn],
   "/droprow": ["post", droprow_fn],
   "/setting": ["get",setting_fn],
   "/settingDone": ["post", settingDone_fn],
   "/writequestion": ["post", writequestion_fn],
   "/writequestioned": ["get", writequestioned_fn],
   "/addscore": ["post", addscore_fn],


}
